package dcc.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;

import dcc.app.Application;
import dcc.mbg.model.User;
import dcc.service.CommonService;
import dcc.tools.BeanFactory;
import dcc.tools.Tools;

public class LoginWindow extends JFrame {

	private Application app;
	private JButton btnLogin;
	private User user;
	private JTextField tfUserName;
	private JPasswordField pfPassword;

	public LoginWindow(Application app) {
		getContentPane().setBackground(UIManager.getColor("window"));
		this.app = app;
		this.user = new User();
		this.setIconImage(new ImageIcon(MainWindow.class
				.getResource("/dcc/resources/images/ico.png")).getImage());
		setResizable(false);
		setTitle(Tools.get().getValue("LABEL_LOGIN_WINDOW_TITLE"));
		setName("");
		setAlwaysOnTop(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int halfWidth = screenSize.width / 2;
		int halfHeight = screenSize.height / 2;
		int width = 236;
		int height = 150;
		setBounds(halfWidth - width / 2, halfHeight - height / 2, width, height);
		getContentPane().setLayout(null);
		initWidget();
		initDataBindings();
		setVisible(true);
	}

	private void login() {
		if ("true".equalsIgnoreCase(Tools.get().getValue("DEVELOPMENT"))) {
			this.app.showMainWindow(new User("developer", "developer",
					"developer", "developer"));
		} else {
			if (this.user.getUserName() == null
					|| this.user.getUserName().equals("")
					|| this.user.getPassword() == null
					|| this.user.getPassword().equals("")) {
				JOptionPane.showMessageDialog(this,
						Tools.get().getValue("LABEL_INPUT_USERNAME_PASSWORD"),
						Tools.get().getValue("LABEL_LOGIN_FAILED"),
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			User currentUser = ((CommonService) BeanFactory
					.getBean("commonService")).checkUser(this.user);
			if (currentUser != null) {
				this.app.showMainWindow(currentUser);
			} else {
				JOptionPane.showMessageDialog(this,
						Tools.get().getValue("LABEL_USERNAME_PASSWORD_ERROR"),
						Tools.get().getValue("LABEL_LOGIN_FAILED"),
						JOptionPane.ERROR_MESSAGE);
			}

		}
	}

	private void initWidget() {
		JLabel lblUserName = new JLabel(Tools.get().getValue("LABEL_USERNAME")
				+ ":");
		lblUserName.setForeground(Color.BLUE);
		lblUserName.setFont(new Font("Dialog", Font.PLAIN, 14));
		lblUserName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUserName.setBounds(10, 26, 54, 18);
		getContentPane().add(lblUserName);

		JLabel lblPassword = new JLabel(Tools.get().getValue("LABEL_PASSWORD")
				+ ":");
		lblPassword.setForeground(Color.BLUE);
		lblPassword.setFont(new Font("Dialog", Font.PLAIN, 14));
		lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPassword.setBounds(10, 54, 54, 18);
		getContentPane().add(lblPassword);

		tfUserName = new JTextField();
		tfUserName.setFont(new Font("Dialog", Font.PLAIN, 14));
		tfUserName.setBounds(74, 24, 146, 22);
		getContentPane().add(tfUserName);

		pfPassword = new JPasswordField();
		pfPassword.setFont(new Font("Dialog", Font.PLAIN, 14));
		pfPassword.setBounds(74, 52, 146, 22);
		getContentPane().add(pfPassword);

		btnLogin = new JButton(Tools.get().getValue("LABEL_LOGIN"));
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
				userNameGetFocus();
			}
		});
		btnLogin.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyChar() == 10) {
					login();
					userNameGetFocus();
				}
			}
		});
		btnLogin.setBackground(UIManager.getColor("Button.background"));
		btnLogin.setFont(new Font("Dialog", Font.PLAIN, 14));
		btnLogin.setBounds(148, 84, 72, 28);
		getContentPane().add(btnLogin);
	}

	private void userNameGetFocus() {
		this.tfUserName.requestFocus();
	}

	protected void initDataBindings() {
		BeanProperty<User, String> userNameProperty = BeanProperty
				.create("userName");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty
				.create("text");
		AutoBinding<User, String, JTextField, String> userNameAutoBinding = Bindings
				.createAutoBinding(UpdateStrategy.READ_WRITE, user,
						userNameProperty, tfUserName, jTextFieldBeanProperty);
		userNameAutoBinding.bind();

		BeanProperty<User, String> passwordProperty = BeanProperty
				.create("password");
		BeanProperty<JPasswordField, String> jPasswordFieldBeanProperty = BeanProperty
				.create("text");
		AutoBinding<User, String, JPasswordField, String> passwordAutoBinding = Bindings
				.createAutoBinding(UpdateStrategy.READ_WRITE, user,
						passwordProperty, pfPassword,
						jPasswordFieldBeanProperty);
		passwordAutoBinding.bind();
	}

	public void close() {
		this.setVisible(false);
	}
}
